import datetime

from django.db.models.signals import pre_save
from django.dispatch import receiver

from .models import Task


@receiver(pre_save, sender=Task)
def before_save(sender, **kwargs):
    if not kwargs.get('created'):
        return
    instance: Task = kwargs.get('instance')

    if not instance.doing_time:
        instance.doing_time = instance.created_time + datetime.timedelta(seconds=instance.deadline)
